package com.ruyuan.dependency.product.controller;

import com.alibaba.fastjson.JSON;
import com.ruyuan.dependency.product.entity.Brand;
import com.ruyuan.dependency.product.entity.Category;
import com.ruyuan.dependency.product.entity.Sku;
import com.ruyuan.dependency.product.entity.Spu;
import com.ruyuan.dependency.product.service.BrandService;
import com.ruyuan.dependency.product.service.CategoryService;
import com.ruyuan.dependency.product.service.SkuService;
import com.ruyuan.dependency.product.service.SpuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhonghuashishan
 */
@Slf4j
@RestController
@RequestMapping("/mockProduct")
public class MockProductController {

    @Autowired
    private SkuService skuService;

    @Autowired
    private SpuService spuService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping()
    public Boolean mockProduct() {
        Brand huaweiBrand = Brand.builder().name("华为")
                .logo("https://fake-image.com/brand/logo/brand-logo-huawei.png")
                .build();
        Brand lenovoBrand = Brand.builder().name("联想")
                .logo("https://fake-image.com/brand/logo/brand-logo-lenovo.png")
                .build();
        Brand dellBrand = Brand.builder().name("戴尔")
                .logo("https://fake-image.com/brand/logo/brand-logo-dell.png")
                .build();
        brandService.save(huaweiBrand);
        brandService.save(lenovoBrand);
        brandService.save(dellBrand);

        Category phoneCategory = Category.builder().name("手机").build();
        Category laptopCategory = Category.builder().name("笔记本").build();
        Category serverCategory = Category.builder().name("服务器").build();
        categoryService.save(phoneCategory);
        categoryService.save(laptopCategory);
        categoryService.save(serverCategory);

        Spu mate40proSPU = Spu.builder().brandId(huaweiBrand.getId()).categoryId(phoneCategory.getId()).name("mate40pro").build();
        Spu p15Spu = Spu.builder().brandId(lenovoBrand.getId()).categoryId(laptopCategory.getId()).name("p15").build();
        Spu r740Spu = Spu.builder().brandId(lenovoBrand.getId()).categoryId(serverCategory.getId()).name("r740").build();
        spuService.save(mate40proSPU);
        spuService.save(p15Spu);
        spuService.save(r740Spu);

        int count = 1_00;
        AtomicInteger skuSequencer = new AtomicInteger(0);
        for (int i = 1; i <= count; i++) {
            Sku mate40proSku = Sku.builder()
                    .spuId(mate40proSPU.getId())
                    .name("mate40-sku-" + skuSequencer.incrementAndGet())
                    .price(5000L)
                    .seckillingPrice(3999L)
                    .currentStock(100_0000)
                    .lockedStock(0)
                    .image("https://fake-image.com/sku/mate40pro-1.png")
                    .images(String.join(",",
                            "https://fake-image.com/sku/mate40pro-2.png",
                            "https://fake-image.com/sku/mate40pro-3.png",
                            "https://fake-image.com/sku/mate40pro-4.png",
                            "https://fake-image.com/sku/mate40pro-5.png",
                            "https://fake-image.com/sku/mate40pro-6.png"
                            )
                    )
                    .build();
            Sku p15Sku = Sku.builder()
                    .spuId(p15Spu.getId())
                    .name("p15-sku-" + skuSequencer.incrementAndGet())
                    .price(7000L)
                    .seckillingPrice(5999L)
                    .currentStock(100_0000)
                    .lockedStock(0)
                    .image("https://fake-image.com/sku/p15-1.png")
                    .images(String.join(",",
                            "https://fake-image.com/sku/p15-2.png",
                            "https://fake-image.com/sku/p15-3.png",
                            "https://fake-image.com/sku/p15-4.png",
                            "https://fake-image.com/sku/p15-5.png"
                            )
                    ).build();
            Sku r740Sku = Sku.builder()
                    .spuId(r740Spu.getId())
                    .name("r740-sku-" + skuSequencer.incrementAndGet())
                    .price(15000L)
                    .seckillingPrice(13999L)
                    .currentStock(100_0000)
                    .lockedStock(0)
                    .image("https://fake-image.com/sku/r740-1.png")
                    .images(String.join(",",
                            "https://fake-image.com/sku/r740-2.png",
                            "https://fake-image.com/sku/r740-3.png",
                            "https://fake-image.com/sku/r740-4.png",
                            "https://fake-image.com/sku/r740-5.png"
                            )
                    )
                    .build();
            skuService.save(mate40proSku);
            log.info("保存sku, sku={}", JSON.toJSONString(mate40proSku));
            skuService.save(p15Sku);
            log.info("保存sku, sku={}", JSON.toJSONString(p15Sku));
            skuService.save(r740Sku);
            log.info("保存sku, sku={}", JSON.toJSONString(r740Sku));
        }

        return Boolean.TRUE;
    }
}
